const XLSX = require('xlsx');
const fs = require('fs'); //文件模块
const Path = require('path'); //系统路径模块

class XlsClass {
  constructor(path) {
    this.xlsPath = path;
  }

  getHeaderRow(sheet) {
    const headers = []
    const range = XLSX.utils.decode_range(sheet['!ref'])
    let C
    const R = range.s.r
    /* start in the first row */
    for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
      const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
      /* find the cell in the first row */
      let hdr = 'UNKNOWN ' + C // <-- replace with your desired default
      if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
      headers.push(hdr)
    }
    return headers
  }

  readData() {
    const workbook = XLSX.readFile(this.xlsPath, { type: 'array' })
    const firstSheetName = workbook.SheetNames[0]
    const worksheet = workbook.Sheets[firstSheetName]
    const results = XLSX.utils.sheet_to_json(worksheet)
    console.log('result', results)
    this.generateI18nFiles(results)
  }

  generateI18nFiles(results) {
    const enData  = {};
    const zhData = {};
    const hkData = {};

    const arData = {};
    const csData = {};
    const daData = {};
    const deData = {};
    const elData = {};
    const esData = {};
    const faData = {};
    const fiData = {};
    const frData = {};
    const gaData = {};
    const heData = {};
    const hrData = {};
    const huData = {};
    const itData = {};
    const jaData = {};
    const koData = {};
    const nbData = {};
     
    const plData = {};
    const ptData = {};
    const roData = {};
    const ruData = {};
    const skData = {};
    const svData = {};
    const thData = {};
    const trData = {};
    const ukData = {};
    const viData = {};
    const inData = {};

    results.forEach(element => {
      
      enData[element.key.trim()]= element.en;
      zhData[element.key.trim()] = element.cn;
      hkData[element.key.trim()] = element.hk;

      frData[element.key.trim()] = element.fr;
      itData[element.key.trim()] = element.it;
      esData[element.key.trim()] = element.es;
      ptData[element.key.trim()] = element.pt;
      jaData[element.key.trim()] = element.ja;
      deData[element.key.trim()] = element.de;
      plData[element.key.trim()] = element.pl;
      huData[element.key.trim()] = element.hu;
      ruData[element.key.trim()] = element.ru;
      ukData[element.key.trim()] = element.uk;
      koData[element.key.trim()] = element.ko;
      viData[element.key.trim()] = element.vi;
      thData[element.key.trim()] = element.th;
      hrData[element.key.trim()] = element.hr;
      svData[element.key.trim()] = element.sv;
      nbData[element.key.trim()] = element.nb;
      daData[element.key.trim()] = element.da;
      fiData[element.key.trim()] = element.fi;
      csData[element.key.trim()] = element.cs;
      skData[element.key.trim()] = element.sk;
      gaData[element.key.trim()] = element.ga;
      arData[element.key.trim()] = element.ar;
      heData[element.key.trim()] = element.he;
      trData[element.key.trim()] = element.tr;
      roData[element.key.trim()] = element.ro;
      elData[element.key.trim()] = element.el;
      faData[element.key.trim()] = element.fa;

      inData[element.key.trim()] = element.in;
 

    });

    this.writeJson('../lang/en.json', JSON.stringify(enData));
    this.writeJson('../lang/zh.json', JSON.stringify(zhData));
    this.writeJson('../lang/hk.json', JSON.stringify(hkData));

    this.writeJson('../lang/fr.json', JSON.stringify(frData));
    this.writeJson('../lang/it.json', JSON.stringify(itData));
    this.writeJson('../lang/es.json', JSON.stringify(esData));
    this.writeJson('../lang/pt.json', JSON.stringify(ptData));
    this.writeJson('../lang/ja.json', JSON.stringify(jaData));
    this.writeJson('../lang/de.json', JSON.stringify(deData));
    this.writeJson('../lang/pl.json', JSON.stringify(plData));
    this.writeJson('../lang/hu.json', JSON.stringify(huData));
    this.writeJson('../lang/ru.json', JSON.stringify(ruData));
    this.writeJson('../lang/uk.json', JSON.stringify(ukData));
    this.writeJson('../lang/ko.json', JSON.stringify(koData));
    this.writeJson('../lang/vi.json', JSON.stringify(viData));
    this.writeJson('../lang/th.json', JSON.stringify(thData));
    this.writeJson('../lang/hr.json', JSON.stringify(hrData));
    this.writeJson('../lang/sv.json', JSON.stringify(svData));
    this.writeJson('../lang/nb.json', JSON.stringify(nbData));
    this.writeJson('../lang/da.json', JSON.stringify(daData));
    this.writeJson('../lang/fi.json', JSON.stringify(fiData));
    this.writeJson('../lang/cs.json', JSON.stringify(csData));
    this.writeJson('../lang/sk.json', JSON.stringify(skData));
    this.writeJson('../lang/ga.json', JSON.stringify(gaData));
    this.writeJson('../lang/ar.json', JSON.stringify(arData));
    this.writeJson('../lang/he.json', JSON.stringify(heData));
    this.writeJson('../lang/fa.json', JSON.stringify(faData));
    this.writeJson('../lang/tr.json', JSON.stringify(trData));
    this.writeJson('../lang/ro.json', JSON.stringify(roData));
    this.writeJson('../lang/el.json', JSON.stringify(elData));
    this.writeJson('../lang/in.json', JSON.stringify(inData));
 

  }

  writeJson(path, content) {
    const targetFile = Path.join(__dirname, path);

    fs.writeFile(targetFile, content, function (err) {
      if (err) {
        return console.error('error:' + err);
      }
      console.log('文件创建成功，地址：' + targetFile);
    });

  }
}

module.exports = XlsClass